define(['app'],function(app){return {module: function($scope, $http, eventBusService, httpService, params) {

	$scope.menu = {};

	//接收数据刷新
	eventBusService.subscribe('content.page.reload', function(event,data) {
		$scope.find();
	});

	if(params.father_id!='undefined'){
		httpService.post(app.urls.menu_select,{"id":params.father_id},function(data){
			if("0000" === data.code){
				$scope.menu_name = data.data[0].name;
				$scope.findButton(data.data[0].id);
			}else{
				alert(data.msg);
			}
		});
	}else {
		$scope.form.type=params.type;
	}

	$scope.findButton=function(menu_id){
		httpService.post(app.urls.button_select,{"menu_id":menu_id},function(data){
			if("0000" === data.code){
				$scope.buttons = data.data;
			}else{
				alert(data.msg);
			}
		});
	}

	//按钮事件
	$scope.operate = function(event) {
		$scope[event]();
	}

	$scope.add = function (){
		eventBusService.publish("modal.page","modules/content/staff/dimission/add?v="+Math.random()+"&fk_menuid="+params.fk_menuid+ "&father_id=" + params.father_id);
	}

	$scope.cancel = function (){
		var values = [];
		var ids = "";
		$('#' + $scope.controllerName + ' input[name="dataPk"]:checked').each(function () {
			var arr = $(this).val().split(',');
			values.push(arr[0]);
			ids = ids + ",'" + arr[0] + "'";
		});
		let fk_teachers = "";
		let is_edit = true;
		values.forEach(function (item, index, arr){
			let dimission = $scope.rows.find(myItem =>{
				return myItem.id == arr[index];
			});
			fk_teachers = fk_teachers + ",'" + dimission.fk_teacher + "'";
			if (dimission.is_cancel == '1'){
				is_edit = false;
				return;
			}
		})
		if(values.length < 1) {
			eventBusService.publish('modal.alert', {"title": "操作提示", "content": "请选择数据。"});
		}else {
			if (is_edit){
				eventBusService.publish("modal.alert",{"content":"是否撤销？","isAutoClose":false,"confirm":function(){
					httpService.post(app.urls.dimission_update, {"is_cancel": "1","ids": ids.replace(',', '')},function(data){
						if("0000" === data.code){
							httpService.post(app.urls.updateTeacher, {"status":"0","ids": fk_teachers.replace(',', '')},function(data){});
							$scope.find();
						}else{
							alert(data.msg);
						}
					});
				},"cancel":function(){
					//用户取消，不做操作
				}});
			}else{
				eventBusService.publish('modal.alert', {"title":"操作提示","content":"请勿重复操作。"});
			}
		}
	}

	//按钮点击事件-条件查询
	$scope.select = function(){
		$scope.page.current = 1;
		$scope.find();
	}

	$scope.find = function(){
		httpService.postpage(app.urls.dimission_select,$scope.form,function(data){
			if("0000" === data.code){
				$scope.rows = data.data;
			}else{
				alert(data.msg);
			}
		});
	}

	let init = function (){
		$scope.find();
	}

	init();
		//选择数据
		$scope.optData = function (element) {
			if($(element).find("input[name='dataPk']").is(":checked")){
				$(element).find("input[name='dataPk']").prop("checked",false);
				$(element).removeClass("opt_tr");
			}else{
				$(element).find("input[name='dataPk']").prop("checked",true);
				$(element).addClass("opt_tr");
			}
		}
		$scope.checkAll = function(target) {
			$("#" + $scope.controllerName + " input[name='dataPk']").each(function() {
				if ($(this).prop("checked")) {
					$(this).prop('checked', false);
					$(this).parents("tr").removeClass("opt_tr");
				} else {
					$(this).prop('checked', true);
					$(this).parents("tr").addClass("opt_tr");
				}
			})
		}
}}});